let Main = {
    data() {
        var valiNumberPass1 = (rule, value, callback) => {//包含小数的数字
            let reg = /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g;
            if (value === '') {
                callback(new Error('请输入内容'));
            } else if (!reg.test(value)) {
                callback(new Error('请输入数字可包含两位小数'));
            } else if (value <= 0) {
                callback(new Error('必须大于零'));
            } else {
                callback();
            }
        };
        var valiNumberPass2 = (rule, value, callback) => {//正整数
            let reg = /^[+]{0,1}(\d+)$/g;
            if (value === '') {
                callback(new Error('请输入内容'));
            } else if (!reg.test(value)) {
                callback(new Error('请输入0及0以上的整数'));
            } else if (value <= 0) {
                callback(new Error('必须大于零'));
            } else {
                callback();
            }
        };
        return {
            // 校验规则
            sub1Rules: {
                reserve: [
                    {required: true, validator: valiNumberPass2, trigger: "blur"}
                ], priceB: [
                    {required: true, validator: valiNumberPass1, trigger: "blur"}
                ], priceA: [
                    {required: true, validator: valiNumberPass1, trigger: "blur"}
                ], priceD: [
                    {required: true, validator: valiNumberPass1, trigger: "blur"}
                ],
            },
            isGoodsWarehousing: false,
            isProductManagement: true,
            Quagga: Quagga,
            productForm: {
                barcode: '',
                productName: '',
                brand: '',
                spec: '',
                originCountry: '',
                company: '',
                priceB: '',
                priceA: '',
                priceD: '',
            },
            productFormDisabled: false,
            productTableData: [],
            PagingTotal: 0,
            currentPage: 1,
            queryProductName: '',
            queryForm: {
                queryProductName: '',
                queryBrand: '',
                queryProductItem: '',
                queryCompany: '',
                queryReserve: '',
            },
            maxReserve: 10000,
            // 商品信息修改
            updateInfoForm: {
                barcode: '',
                productName: '',
                brand: '',
                spec: '',
                originCountry: '',
                company: '',
                priceB: '',
                priceA: '',
                priceD: '',
            },
            modifyWindowVisible: false,
            myHerders: {headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('apmToken')
                }
            }
        }
    },
    created() {
        this.ProductManagement();
        let _this = this;
        axios.get('/apm/product/queryMaxReserve', _this.myHerders)
            .then(function (resp) {
                _this.maxReserve = resp.data.count;
            })
    },
    methods: {
        // 扫码入库
        GoodsWarehousing() {
            this.isProductManagement = false;
            this.isGoodsWarehousing = true;
            this.QuaggaInit();
        },
        // 扫码商品识别码
        QuaggaInit() {
            let _this = this;
            this.Quagga.init({
                inputStream: {
                    name: "Live",
                    type: "LiveStream",
                    target: document.querySelector('#canvanCode')
                },
                decoder: {
                    readers: ["ean_reader", 'code_39_reader'],
                    debug: {
                        drawBoundingBox: false,
                        showFrequency: false,
                        drawScanline: false,
                        showPattern: false,
                    },
                    multiple: false
                },
                frequency: 1,
            }, () => {
                //打开摄像头
                Quagga.start();
                Quagga.onDetected(function (data) {
                    _this.productForm['barcode'] = data.codeResult.code;
                    //data.codeResult.code就是摄像头扫描出的商品条形码
                    _this.$refs.audio.play();
                    if (_this.productForm['barcode'].length === 13) {
                        axios.get("/apm/commodity/queryProductIn?codeNumber=" + _this.productForm['barcode'], _this.myHerders)
                            .then(function (resp) {
                                // 扫描商品成功后添加商品
                                if (resp.data.isSucc) {
                                    _this.productForm = resp.data.message;
                                    _this.productFormDisabled = resp.data.productFormDisabled;
                                }
                            }).catch(function (error) {
                            _this.$message.error(error);
                        })
                    }
                });
            });
        },
        QuaggaStop() {
            try {
                this.Quagga.stop();
            } catch (error) {
                console.log(error);
            }
        },
        // 入库信息提交
        productSubmit() {
            let _this = this;
            this.$refs['productForm'].validate((valid) => {  //开启校验
                if (valid) {   // 如果校验通过，请求接口，允许提交表单
                    axios.post('/apm/commodity/enterProductIn', _this.productForm, _this.myHerders)
                        .then(function (resp) {
                            if (resp.data.isSucc === 200) {
                                _this.$message.success('加入成功');
                                _this.productForm = {
                                    barcode: '',
                                    productName: '',
                                    brand: '',
                                    spec: '',
                                    company: '',
                                    originCountry: '',
                                    priceB: '',
                                    priceA: '',
                                    priceD: '',
                                };
                                _this.productFormDisabled = false;
                            } else {
                                _this.$message.error(resp.data.error);
                            }
                        }).catch(function (e) {
                        _this.$message.error(e);
                    })
                } else {   //校验不通过
                    return false;
                }
            })
        },
        // 商品管理面板
        ProductManagement() {
            let _this = this;
            this.isGoodsWarehousing = false;
            this.QuaggaStop();
            this.isProductManagement = true;
            this.sendSelectTableInfo(1, '', '', '', '', this.queryForm.queryReserve);
        },
        sendSelectTableInfo(startPaging, ProductName, Brand, Company, ProductItem, Reserve) {
            let _this = this;
            axios.get('/apm/product/queryPaging?startPaging=' + startPaging +
                "&queryProductName=" + ProductName +
                "&queryBrand=" + Brand +
                "&queryCompany=" + Company +
                "&queryProductItem=" + ProductItem +
                "&queryReserve=" + Reserve, _this.myHerders)
                .then(function (resp) {
                    _this.productTableData = resp.data.data;
                    _this.PagingTotal = resp.data.PagingTotal;
                }).catch(function (e) {
                _this.$message.error(e);
            });
        },
        // 表格数据格式化
        formatNumber(row, column) {
            let property = column['property']
            return (row[property] / 100).toFixed(2);
        },
        formatTo2(value) {
            return (value / 100).toFixed(2);
        },
        // 点击表格事件
        handleCellClick(row, column, cell, event) {
            this.modifyWindowVisible = true;
            this.updateInfoForm = row;
        },
        // 提交修改
        modifyWindowSubmit() {
            let _this = this;
            axios.post('/apm/commodity/modifyProduct', _this.updateInfoForm, _this.myHerders)
                .then(function (resp) {
                    if (resp.data.isSucc === 200) {
                        _this.$message.success("商品更新成功");
                        _this.modifyWindowVisible = false
                        _this.sendSelectTableInfo(_this.currentPage, _this.queryForm.queryProductName, _this.queryForm.queryBrand,
                            _this.queryForm.queryCompany, _this.queryForm.queryProductItem, _this.queryForm.queryReserve);
                    } else {
                        _this.$message.error(resp.data.error);
                    }
                }).catch(function (e) {
                _this.$message.error(e);
            })
        },
        // 页面改变时
        currentChange() {
            let _this = this;
            this.sendSelectTableInfo(this.currentPage, this.queryForm.queryProductName,
                this.queryForm.queryBrand, this.queryForm.queryCompany, this.queryForm.queryProductItem, this.queryForm.queryReserve);
        },
        productNameLoad(queryString, cb) {
            if (queryString === '') {
                return;
            }
            this.sendFuzzy("productName", queryString, cb);
        },
        productBrandLoad(queryString, cb) {
            if (queryString === '') {
                return;
            }
            this.sendFuzzy("productBrand", queryString, cb);
        },
        productCompanyLoad(queryString, cb) {
            if (queryString === '') {
                return;
            }
            this.sendFuzzy("productCompany", queryString, cb);
        },
        productOriginCountryLoad(queryString, cb) {
            if (queryString === '') {
                return;
            }
            this.sendFuzzy("productProductItem", queryString, cb);
        },
        sendFuzzy(queryParam, queryString, cb) {
            let _this = this;
            axios.get('/apm/product/queryFuzzy?queryName=' + queryParam + "&queryValue=" + queryString, _this.myHerders)
                .then(function (resp) {
                    let temp = resp.data;
                    var results = queryString ? temp : [{'value': ''}];
                    // 调用 callback 返回建议列表的数据
                    cb(results);
                })
                .catch(function (e) {
                    _this.$message.error(e);
                })
        },
        // 查询商品
        onSubmitQuery() {
            this.sendSelectTableInfo(1, this.queryForm.queryProductName, this.queryForm.queryBrand, this.queryForm.queryCompany,
                this.queryForm.queryProductItem, this.queryForm.queryReserve);
        },
        // 删除商品
        removeWindowSubmit() {
            let _this = this;
            axios.post('/apm/commodity/removeProduct', _this.updateInfoForm, _this.myHerders)
                .then(function (resp) {
                    if (resp.data.isSucc === 200) {
                        _this.$message.success("商品删除成功");
                        _this.modifyWindowVisible = false
                        _this.sendSelectTableInfo(_this.currentPage, _this.queryForm.queryProductName, _this.queryForm.queryBrand,
                            _this.queryForm.queryCompany, _this.queryForm.queryProductItem, _this.queryForm.queryReserve);
                    } else {
                        _this.$message.error(resp.data.error);
                    }
                }).catch(function (e) {
                _this.$message.error(e);
            })
        }
    },
}
let Ctor = Vue.extend(Main)
new Ctor().$mount('#app')